/*
* $Id: QueenBeeForm.java 270 2011-02-20 13:59:40Z volok-aleksej@yandex.ru $
*
* Copyright 2008-2011 by Volokitin Alexey
* All rights reserved
*
*/
package apiary.ui.journal;

import apiary.common.FormHelper;
import java.util.*;
import javax.microedition.lcdui.*;

import apiary.Apiary;
import apiary.component.journal.Wizard;
import apiary.db.FamilyStore.FamilyRecord;
import apiary.db.PointStore.PointRecord;
import apiary.db.QueenBeeStore.QueenBeeRecord;
import apiary.db.managers.DBManager;
import apiary.ui.CategoryItem;
import apiary.ui.ItemEventListener;

public class QueenBeeForm extends WizardForm implements ItemEventListener,
                                                        ItemStateListener
{
    public class QueenBeeValue
    {
        public String nameQueenBee;
        public String description;
        public int idFamily;
        public int idParentQueenBee;
        public Date date;
        public int breed;
        public String fert;
        public boolean isDead;

        public QueenBeeValue() {
            nameQueenBee = new String();
            description = new String();
            date = new Date();
            fert = new String();
            isDead = false;
        }
    }

    private Command chooseCommand;
    private CategoryItem curCategory;
    private int idQB;

    public QueenBeeForm(String name, Integer id, Wizard wiz, boolean nextCommand) {
        super(name, wiz, id, nextCommand);
        idQB = 0;
        Apiary apiary = Apiary.getInstance();
        append(new TextField(apiary.getLocaleString("NameQueenBee"), "", 256, TextField.ANY));
        append(apiary.getLocaleString("NameFamily"));
        String nameEmpty = Apiary.getInstance().getLocaleString("NoFamily");
        CategoryItem item = FormHelper.createCategoryItem(this, "family", nameEmpty, "");
        item.setState("0");
        append(item);
        DateField date = new DateField(apiary.getLocaleString("Birthday"), DateField.DATE);
        date.setDate(new Date());
        append(date);
        append(apiary.getLocaleString("NameParentQB"));
        nameEmpty = Apiary.getInstance().getLocaleString("NoParentQB");
        item = FormHelper.createCategoryItem(this, "parent", nameEmpty, "");
        item.setState("0");
        append(item);
        append(apiary.getLocaleString("Breed"));
        Hashtable breeds = DBManager.getInstance().getBreeds();
        nameEmpty = (String) breeds.get(new Integer(QueenBeeRecord.BREED_HYBRID));
        item = FormHelper.createCategoryItem(this, "breed", nameEmpty, "");
        item.setState("0");
        append(item);
        append(new TextField(apiary.getLocaleString("Fertility"), "", 256, TextField.ANY));
        append(new TextField(apiary.getLocaleString("Description"), "", 256, TextField.ANY));

        setItemStateListener(this);
    }

    public Object GetDataForm() {
        QueenBeeValue qbValue = new QueenBeeValue();
        TextField text = (TextField) get(0);
        qbValue.nameQueenBee = text.getString();
        CategoryItem item = (CategoryItem) get(2);
        qbValue.idFamily = Integer.parseInt(item.getState());
        DateField date = (DateField) get(3);
        qbValue.date = date.getDate();
        item = (CategoryItem) get(5);
        qbValue.idParentQueenBee = Integer.parseInt(item.getState());
        item = (CategoryItem) get(7);
        qbValue.breed = Integer.parseInt(item.getState());
        text = (TextField) get(8);
        qbValue.fert = text.getString();
        text = (TextField) get(9);
        qbValue.description = text.getString();
        return qbValue;
    }

    public void setQueenBeeValue(int id, QueenBeeValue value) {
        DBManager dBManager = DBManager.getInstance();
        TextField text = (TextField) get(0);
        text.setString(value.nameQueenBee);
        CategoryItem item = (CategoryItem) get(2);
        item.setState(String.valueOf(value.idFamily));
        FamilyRecord family = dBManager.getFamily(value.idFamily);
        if(family != null)
            item.setName(family.name);
        else
            item.setName(Apiary.getInstance().getLocaleString("NoFamily"));
        DateField date = (DateField) get(3);
        date.setDate(value.date);
        item = (CategoryItem) get(5);
        QueenBeeRecord qb = dBManager.getQueenBee(value.idParentQueenBee);
        if(qb != null) {
            item.setName(qb.nameQueenBee);
            item.setState(String.valueOf(value.idParentQueenBee));
        } else {
            String nameEmpty = Apiary.getInstance().getLocaleString("NoParentQB");
            item.setName(nameEmpty);
            item.setState("0");
        }
        item = (CategoryItem) get(7);
        item.setState(String.valueOf(value.breed));
        Hashtable breeds = dBManager.getBreeds();
        item.setName((String) breeds.get(new Integer(value.breed)));
        text = (TextField) get(8);
        text.setString(value.fert);
        text = (TextField) get(9);
        text.setString(value.description);
        idQB = id;
    }
    
    protected void commandUser(Command command) {
        itemNotifyEvent(curCategory, PRESS);
    }

    public void itemNotifyEvent(Item item, String event) {
        if(event.equals(PRESS)){
            Apiary apiary = Apiary.getInstance();
            String name = "";
            CategoryItem categoryItem = (CategoryItem) item;
            if(categoryItem.getId().equals("family"))
                name = apiary.getLocaleString("ChooseFamily");
            if(categoryItem.getId().equals("parent"))
                name = apiary.getLocaleString("ChooseQueenBee");
            else if(categoryItem.getId().equals("breed"))
                name = apiary.getLocaleString("Breed");
            ChooseList list = new ChooseList(name, item);
            if(categoryItem.getId().equals("family"))
                FormHelper.fillFamilyList(list);
            if(categoryItem.getId().equals("parent"))
                fillParentList(list);
            else if(categoryItem.getId().equals("breed"))
                fillBreedList(list);
            list.setBackScreen(this);
            apiary.Display(list);
        }
    }

    public void itemStateChanged(Item item) {
        if(item.getClass() != CategoryItem.class)
            return;
        CategoryItem categoryItem = (CategoryItem) item;
        Apiary apiary = Apiary.getInstance();
        if(categoryItem.getActive()) {
            chooseCommand = new Command(apiary.getLocaleString("Choose"), Command.BACK, 0);
            addCommand(chooseCommand);
            curCategory = categoryItem;
        } else {
            removeCommand(chooseCommand);
            curCategory = null;
        }
    }

    private void fillBreedList(ChooseList list) {
        Hashtable breeds = DBManager.getInstance().getBreeds();
        Enumeration typesEnum = breeds.keys();
        while(typesEnum.hasMoreElements()) {
            Integer type = (Integer) typesEnum.nextElement();
            String typeName = (String) breeds.get(type);
            CategoryItem item = FormHelper.createCategoryItem(list,
                    String.valueOf(type), typeName, "");
            list.insert(0, item);
        }
    }

    private void fillParentList(ChooseList list) {
        String nameEmpty = Apiary.getInstance().getLocaleString("NoParentQB");
        CategoryItem item = FormHelper.createCategoryItem(list, "0", nameEmpty, "");
        list.append(item);
        fillParentList(list, 0);
        DBManager dBManager = DBManager.getInstance();
        Vector points = dBManager.getPoints();
        for(int i = 0; i < points.size(); i++) {
            PointRecord point = (PointRecord) points.elementAt(i);
            Vector families = dBManager.getFamilies(point.idPoint);
            for(int j = 0; j < families.size(); j++) {
                FamilyRecord family = (FamilyRecord) families.elementAt(j);
                int listSize = list.size();
                if(fillParentList(list, family.idFamily)) {
                    StringItem stringItem = new StringItem("",
                            point.namePoint + ": " + family.name);
                    list.insert(listSize, stringItem);
                }
            }
        }
    }
    
    private boolean fillParentList(ChooseList list, int idFamily) {
        DBManager dBManager = DBManager.getInstance();
        Vector qbees = dBManager.getQueenBees(idFamily);
        if(qbees.isEmpty())
            return false;
        int size = 0;
        for(int i = 0; i < qbees.size(); i++) {
            QueenBeeRecord qBRecord = (QueenBeeRecord) qbees.elementAt(i);
            if(idQB == qBRecord.idQueenBee)
                continue;
            String id = String.valueOf(qBRecord.idQueenBee);
            CategoryItem item = FormHelper.createCategoryItem(list,
                    id, qBRecord.nameQueenBee, "");
            list.append(item);
            size++;
        }
        return size != 0;
    }
}
